{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6ae83719",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run -i \"../util/file_utils.ipynb\"\n",
    "%run -i \"../util/lang_utils.ipynb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e227f16b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sentence = 'I have seldom heard him mention her under any other name.'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdb47a29",
   "metadata": {},
   "source": [
    "# Dependency parsing using spaCy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "497e1306",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_dependencies(sentence, model):\n",
    "    doc = model(sentence)\n",
    "    for token in doc:\n",
    "        print(token.text, \"\\t\", token.dep_, \"\\t\", spacy.explain(token.dep_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3fb1b496",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I \t nsubj \t nominal subject\n",
      "have \t aux \t auxiliary\n",
      "seldom \t advmod \t adverbial modifier\n",
      "heard \t ROOT \t root\n",
      "him \t nsubj \t nominal subject\n",
      "mention \t ccomp \t clausal complement\n",
      "her \t dobj \t direct object\n",
      "under \t prep \t prepositional modifier\n",
      "any \t det \t determiner\n",
      "other \t amod \t adjectival modifier\n",
      "name \t pobj \t object of preposition\n",
      ". \t punct \t punctuation\n"
     ]
    }
   ],
   "source": [
    "print_dependencies(sentence, small_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03b164d3",
   "metadata": {},
   "source": [
    "# Ancestors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "607f056b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_ancestors(sentence, model):\n",
    "    doc = model(sentence)\n",
    "    for token in doc:\n",
    "        print(token.text, [t.text for t in token.ancestors])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c39c3ee9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I ['heard']\n",
      "have ['heard']\n",
      "seldom ['heard']\n",
      "heard []\n",
      "him ['mention', 'heard']\n",
      "mention ['heard']\n",
      "her ['mention', 'heard']\n",
      "under ['mention', 'heard']\n",
      "any ['name', 'under', 'mention', 'heard']\n",
      "other ['name', 'under', 'mention', 'heard']\n",
      "name ['under', 'mention', 'heard']\n",
      ". ['heard']\n"
     ]
    }
   ],
   "source": [
    "print_ancestors(sentence, small_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbe342f2",
   "metadata": {},
   "source": [
    "# Children"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a934d23c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_children(sentence, model):\n",
    "    doc = model(sentence)\n",
    "    for token in doc:\n",
    "        print(token.text,[t.text for t in token.children])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3eb47c7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I []\n",
      "have []\n",
      "seldom []\n",
      "heard ['I', 'have', 'seldom', 'mention', '.']\n",
      "him []\n",
      "mention ['him', 'her', 'under']\n",
      "her []\n",
      "under ['name']\n",
      "any []\n",
      "other []\n",
      "name ['any', 'other']\n",
      ". []\n"
     ]
    }
   ],
   "source": [
    "print_children(sentence, small_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7484cb4d",
   "metadata": {},
   "source": [
    "# Lefts and rights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "03790546",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_lefts_and_rights(sentence, model):\n",
    "    doc = model(sentence)\n",
    "    for token in doc:\n",
    "        print(token.text, [t.text for t in token.lefts], [t.text for t in token.rights])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "708bf612",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I [] []\n",
      "have [] []\n",
      "seldom [] []\n",
      "heard ['I', 'have', 'seldom'] ['mention', '.']\n",
      "him [] []\n",
      "mention ['him'] ['her', 'under']\n",
      "her [] []\n",
      "under [] ['name']\n",
      "any [] []\n",
      "other [] []\n",
      "name ['any', 'other'] []\n",
      ". [] []\n"
     ]
    }
   ],
   "source": [
    "print_lefts_and_rights(sentence, small_model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5594190",
   "metadata": {},
   "source": [
    "# Subtree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "411226b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_subtree(sentence, model):\n",
    "    doc = model(sentence)\n",
    "    for token in doc:\n",
    "        print(token.text, [t.text for t in token.subtree])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f912af88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I ['I']\n",
      "have ['have']\n",
      "seldom ['seldom']\n",
      "heard ['I', 'have', 'seldom', 'heard', 'him', 'mention', 'her', 'under', 'any', 'other', 'name', '.']\n",
      "him ['him']\n",
      "mention ['him', 'mention', 'her', 'under', 'any', 'other', 'name']\n",
      "her ['her']\n",
      "under ['under', 'any', 'other', 'name']\n",
      "any ['any']\n",
      "other ['other']\n",
      "name ['any', 'other', 'name']\n",
      ". ['.']\n"
     ]
    }
   ],
   "source": [
    "print_subtree(sentence, small_model)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
